package com.junzhiit.nengji.cms.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.bo.UserBO;
import com.junzhiit.nengji.cms.service.UserService;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.UserEditVO;
import com.junzhiit.nengji.cms.web.vo.UserVipVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
	private UserService userService;
	
	@ApiOperation(value = "查询所有用户")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO param,UserBO userBO){
		return userService.findAll(param, userBO);
	}
	
	@ApiOperation(value="查询手机号码")
	@RequestMapping(value="/phone/{phone}",method=RequestMethod.GET)
	public ResultVO findByMobilePhone(@PathVariable String phone){
		return userService.findByMobilePhone(phone);
	}
	
	@ApiOperation(value="发送用户绑定手机验证码")
	@RequestMapping(value="/code/{id}",method=RequestMethod.GET)
	public ResultVO sendUserBindCode(@PathVariable String id){
		return userService.sendUserBindCode(id);
	}
	
	@RequestMapping(value="/bind/{id}",method=RequestMethod.PUT)
	public ResultVO shopBindUser(@PathVariable String id,String code,String sessionId){
		AssertUtil.notBlank(id, "id is required");
		AssertUtil.notBlank(code, "authCode is required");
		return userService.shopBindUser(id,code,sessionId);
	}
	
	@RequestMapping(value="/shop",method=RequestMethod.GET)
	public ResultVO getShopBindUser(String sessionId){
		return userService.getShopBindUser(sessionId);
	}
	
	@RequestMapping(value="/unbound/{id}",method=RequestMethod.PUT)
	public ResultVO unBoundUser(@PathVariable String id,String sessionId,String code){
		return userService.unBoundUser(id,sessionId,code);
	}
	
	@RequestMapping(value="/integral",method=RequestMethod.PUT)
	public ResultVO edit(UserEditVO userEditVO){
		AssertUtil.notBlank(userEditVO.getShopLocatedIntegralId(), "shopLocatedIntegralId is required");
		AssertUtil.notNull(userEditVO.getType(), "type is required");
		return userService.edit(userEditVO);
	}
	
	@ApiOperation(value="设置用户身份")
	@RequestMapping(value="/identity",method=RequestMethod.PUT)
	public ResultVO updateUserIdentity(UserVipVO userVipVO){
		AssertUtil.notBlank(userVipVO.getUserId(), "userId is required.");
		AssertUtil.notBlank(userVipVO.getName(), "name is required.");
		AssertUtil.notBlank(userVipVO.getMobilePhone(), "mobilePhone is required.");
		AssertUtil.notNull(userVipVO.getIsPresent(), "isPresent is required");
		AssertUtil.notNull(userVipVO.getVipId(), "vipId is required");
		return userService.updateUserIdentity(userVipVO);
	}
}
